<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class WithdrawalsSupply extends Base {

    /**
     * 提现申请记录
     */
    public function _filter($map)
    {
        View::assign('status',config('status.tx'));
        //获取钱包
        $wallet = logic('Wallet')->getName();
        View::assign('wallet',$wallet);
        View::assign('dztype',logic('Wallet')->getTxType());
    }
    public function index(){
        //$map = $this->_search();
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['user_id'] = input('user_id');
            }
            if(input('username')){
                $map['username'] = input('username');
            }
            $map['tx_type'] = ['eq','s_money'];
            if(input('time_start') && input('time_end')){
                $s_time = strtotime(input('time_start'));
                $e_time = strtotime(input('time_end'));
                $map['add_time'] = ['between',[$s_time,$e_time]];
            }
            if($this->sid){
                $map['w.user_id'] = $this->sid;
            }

            $count = Db::name('withdraw_log')->alias('w')->where($map)->count();

            $field = 'w.*,u.weixin,u.aliaccount,u.bankname,u.bankcarduser,u.bankcardnum';
            $list = Db::name('withdraw_log')
                ->alias('w')
                ->join('suppliers u','w.user_id=u.id')
                ->field($field)
                ->where($map)
                ->order('w.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['tx_type'] = logic('Wallet')->getName($v['tx_type']);
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        View::assign('sid',$this->sid);
        return View::fetch();
    }
    //同意提现
    public function agrees(){
        $ids=I('post.id');
        if(!$ids) {
            return json(['status'=>0,'msg'=>'请选择同意提现的记录']);
        }
        $modtime=time();
        $map=array('id'=>array('in',$ids),'status'=>0);
        $list=Db::name('withdraw_log')->where($map)->field('id,user_id')->select();
        if(!$list){
            return json(['status'=>0,'msg'=>'状态有误']);
        }
        Db::name('withdraw_log')->where($map)->save(array('status'=>1,'edittime'=>$modtime));

        return json(['status'=>1,'msg'=>'审核成功']);
    }
    //拒绝提现
    public function refunds(){
        $ids=I('post.id');
        if(!$ids) {
            return json(['status'=>0,'msg'=>'请选择拒绝提现的记录']);
        }
        $modtime=time();

        $map = array('id'=>array('in',$ids),'status'=>0);
        $list=Db::name('withdraw_log')->where($map)->field('id,user_id,total,tx_type')->select();
        if(!$list){
            return json(['status'=>0,'msg'=>'状态有误']);
        }
        $reason = input('reason');
        Db::startTrans();
        foreach($list as $key=>$val) {
            $rs = Db::name('withdraw_log')->where(array('id'=>$val['id']))->save(array('status'=>2,'edittime'=>$modtime,'reson'=>$reason));
            if(!$rs){
                Db::rollback();
                return json(['status'=>0,'msg'=>'更新状态失败']);
            }
            //返钱
            $result=qianbao_supply($val['user_id'],$val['total'],$val['tx_type'],'拒绝兑换','后台拒绝兑换返回');
            if(!$result['status']){
                Db::rollback();
                return json(['status'=>0,'msg'=>$result['msg']]);
            }
        }
        Db::commit();
        return json(['status'=>1,'msg'=>'拒绝提现成功']);
    }
    /**
     * 导出订单
     */

    public function excel(){
        $page = input('page');
        $limit = input('limit')?:15;
        $map = [];
        if(input('user_id')){
            $map['user_id'] = input('user_id');
        }
        if(input('username')){
            $map['username'] = input('username');
        }
        if(input('time_start') && input('time_end')){
            $s_time = strtotime(input('time_start'));
            $e_time = strtotime(input('time_end'));
            $map['add_time'] = ['between',[$s_time,$e_time]];
        }
        $map['tx_type'] = ['eq','s_money'];
        $wallet = logic('Wallet')->getName();
        $dztype = logic('Wallet')->getTxType();
        $status = config('status.tx');
        $count = Db::name('withdraw_log')->alias('w')->where($map)->count();

        $field = 'w.*,u.weixin,u.aliaccount,u.bankname,u.bankcarduser,u.bankcardnum';
        $list = Db::name('withdraw_log')
            ->alias('w')
            ->join('suppliers u','w.user_id=u.id')
            ->field($field)
            ->where($map)
            ->order('w.id desc ')
            ->page($page,$limit)
            ->select();
        $title = ['ID','会员','申请时间','提现金额','手续费','提现钱包','到账方式','到账金额','开户行','银行卡号','姓名','支付宝','微信','状态','审核时间'];
        $data = [];
        foreach($list as $k=>$v){
            $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            $arr = [
                $v['id'], $v['username'],date('Y-m-d H:i:s',$v['addtime']),$v['money'],$v['fee'],$wallet[$v['tx_type']],$dztype[$v['dz_type']],
                $v['price'],$v['bankname'],$v['bankcardnum'],$v['bankcarduser'],$v['aliaccount'],$v['weixin'],$status[$v['status']],
                $v['edittime']?date('Y-m-d H:i:s',$v['edittime']):''
            ];
            $data[] = $arr;
        }
        return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'title'=>$title]);
    }
}
